const list = [
    {
        name: '王者荣耀',
        age: 8,
        address: '深圳市',
        id: 1
    }
]
function listReducer(preState = list, action) {
    const { type, data } = action
    switch (type) {
        case 'add':
            return [...preState, data]
        case 'delete':
            return preState.filter(item => item.id != data)
        case 'update':
            return preState.map(item => {
                if (item.id == data.id) {
                    return data
                } else {
                    return item
                }
            })
        case 'up':
            return up(preState, data)
        case 'down':
            return down(preState, data)
        default:
            return preState
    }
}
function up(preState, index) {
    let item = preState.splice(index, 1)
    preState.splice(index - 1, 0, ...item)
    return [...preState]
}
function down(preState, index) {
    let item = preState.splice(index, 1)
    preState.splice(index + 1, 0, ...item)
    return [...preState]
}
export default listReducer